package com.zhongjian.technologycal.mall.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.activity.OrderDetailsActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.OrderBean;
import com.zhongjian.technologycal.mall.interfacemall.OrderInterface;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UiUtils;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.ListViewForScrollView;

import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2018/1/21.
 */

public class WaitSendOrderAdapter extends BaseAdapter {
    private Context context;
    private List<OrderBean> forList;
    private Dialog picChooseDialog = null;
    private String mContent = null;
    private int cancelPosition;
    private OrderInterface orderInterface;
    public WaitSendOrderAdapter(Context context, List<OrderBean> forList,OrderInterface orderInterface) {
        this.context = context;
        this.forList = forList;
        this.orderInterface = orderInterface;
    }

    @Override
    public int getCount() {
        return forList.size();
    }

    @Override
    public Object getItem(int position) {
        return forList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = UiUtils.inflate(R.layout.items_orders_list);
            holder.tv_shop_name = convertView.findViewById(R.id.tv_shop_name);
            holder.tv_state = convertView.findViewById(R.id.tv_state);
            holder.lv_goods = convertView.findViewById(R.id.lv_goods);
            holder.tv_goods_count = convertView.findViewById(R.id.tv_goods_count);
            holder.tv_money = convertView.findViewById(R.id.tv_money);
            holder.tv_price = convertView.findViewById(R.id.tv_price);
            holder.tv_express = convertView.findViewById(R.id.tv_express);
            holder.bt_del = convertView.findViewById(R.id.bt_del);
            holder.bt_look_logistics = convertView.findViewById(R.id.bt_look_logistics);
            holder.bt_cancel_order = convertView.findViewById(R.id.bt_cancel_order);
            holder.bt_confirm_get = convertView.findViewById(R.id.bt_confirm_get);
            holder.bt_pay = convertView.findViewById(R.id.bt_pay);
            holder.bt_comment = convertView.findViewById(R.id.bt_comment);
            holder.ll_bt = convertView.findViewById(R.id.ll_bt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_shop_name.setText(forList.get(position).getData().get(position).getCreatetime());
        holder.tv_goods_count.setText("" + forList.get(position).getData().get(position).getTolnum());
        holder.tv_price.setText("" + forList.get(position).getData().get(position).getTotalamount());
        //订单状态订单状态 -3=退货完成 -2=订单取消 -1=等待支付 0=已支付，等待发货 1=已发货 2=确认收货 3=评价(完成) 4=申请退货 5=申请取消
        if (forList.get(position).getData().get(position).getCurstatus() == -2) {
            holder.tv_state.setText("等待买家付款");

            holder.bt_cancel_order.setVisibility(View.VISIBLE);
            holder.bt_pay.setVisibility(View.VISIBLE);

            holder.bt_del.setVisibility(View.GONE);
            holder.bt_look_logistics.setVisibility(View.GONE);
            holder.bt_confirm_get.setVisibility(View.GONE);
            holder.bt_comment.setVisibility(View.GONE);

            holder.ll_bt.setVisibility(View.VISIBLE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 0) {
            holder.tv_state.setText("等待卖家发货");
            holder.bt_pay.setVisibility(View.VISIBLE);
            holder.bt_pay.setText("申请退款");
            holder.bt_pay.setVisibility(View.VISIBLE);
            holder.bt_cancel_order.setVisibility(View.GONE);
            holder.bt_del.setVisibility(View.GONE);
            holder.bt_look_logistics.setVisibility(View.GONE);
            holder.bt_confirm_get.setVisibility(View.GONE);
            holder.bt_comment.setVisibility(View.GONE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 2) {
            holder.tv_state.setText("等待买家收货");
            holder.bt_look_logistics.setVisibility(View.VISIBLE);
            holder.bt_confirm_get.setVisibility(View.VISIBLE);
            holder.bt_del.setVisibility(View.GONE);
            holder.bt_cancel_order.setVisibility(View.GONE);
            holder.bt_pay.setVisibility(View.GONE);
            holder.bt_comment.setVisibility(View.GONE);
            holder.ll_bt.setVisibility(View.VISIBLE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 3) {
            holder.tv_state.setText("交易成功");
            holder.ll_bt.setVisibility(View.GONE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 4) {
            holder.tv_state.setText("交易取消");
            holder.bt_del.setVisibility(View.VISIBLE);

            holder.bt_comment.setVisibility(View.GONE);
            holder.bt_cancel_order.setVisibility(View.GONE);
            holder.bt_pay.setVisibility(View.GONE);
            holder.bt_look_logistics.setVisibility(View.GONE);
            holder.bt_confirm_get.setVisibility(View.GONE);

            holder.ll_bt.setVisibility(View.VISIBLE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 5) {
            holder.tv_state.setText("交易关闭");
            holder.bt_del.setVisibility(View.VISIBLE);
            holder.bt_comment.setVisibility(View.GONE);
            holder.bt_cancel_order.setVisibility(View.GONE);
            holder.bt_pay.setVisibility(View.GONE);
            holder.bt_look_logistics.setVisibility(View.GONE);
            holder.bt_confirm_get.setVisibility(View.GONE);

            holder.ll_bt.setVisibility(View.VISIBLE);
        } else if (forList.get(position).getData().get(position).getCurstatus() == 6) {
            holder.tv_state.setText("等待卖家处理");
            holder.ll_bt.setVisibility(View.GONE);
        }
        final MyAdapter adapter = new MyAdapter(forList.get(position).getData().get(position).getOrderlines());
        holder.lv_goods.setAdapter(adapter);
        //按钮点击
        holder.bt_del.setOnClickListener(new BtOnclickListener(position));
        holder.bt_look_logistics.setOnClickListener(new BtOnclickListener(position));
        holder.bt_cancel_order.setOnClickListener(new BtOnclickListener(position));
        holder.bt_confirm_get.setOnClickListener(new BtOnclickListener(position));
        holder.bt_pay.setOnClickListener(new BtOnclickListener(position));
        holder.bt_comment.setOnClickListener(new BtOnclickListener(position));
        holder.tv_shop_name.setOnClickListener(new BtOnclickListener(position));
        //条目点击
        holder.lv_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int mposition, long id) {
                Intent intent = new Intent(context, OrderDetailsActivity.class);
                intent.putExtra("orderId", forList.get(position).getData().get(position).getId());
                intent.putExtra("orderNo", forList.get(position).getData().get(position).getOrderno());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    /**
     * 按钮点击
     */
    private class BtOnclickListener implements View.OnClickListener {
        final private int position;

        public BtOnclickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //店铺名字
                case R.id.tv_shop_name:
//                    Intent intent = new Intent(context, ShopActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent.putExtra("supplierId",forList.get(position).supplier_id);
//                    context.startActivity(intent);
                    break;
                //删除订单
                case R.id.bt_del:

                    break;
                //查看物流
                case R.id.bt_look_logistics:
//                    //Toast.makeText(context, "查看物流", Toast.LENGTH_SHORT).show();
//                    Intent intent2 = new Intent(context, LogisticsMessageActivity.class);
//                    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent2.putExtra("bean",forList.get(position));
//                    context.startActivity(intent2);
                    break;
                //取消订单
                case R.id.bt_cancel_order:

                    break;
                //确认收货
                case R.id.bt_confirm_get:

                    break;
                //付款
                case R.id.bt_pay:
                    cancelPosition = position;
                    initshowDialog();
                    break;
                //评价
                case R.id.bt_comment:

                    break;
            }
        }
    }

    private class ViewHolder {
        TextView tv_shop_name;//店铺名字

        TextView tv_state;//商品状态

        ListViewForScrollView lv_goods;//商品列表

        TextView tv_goods_count;//商品数量

        TextView tv_money;//人民币符号

        TextView tv_price;//商品价格

        TextView tv_express;//快递费

        TextView bt_del;//删除订单

        TextView bt_look_logistics;//查看物流

        TextView bt_cancel_order;//取消订单

        TextView bt_confirm_get;//确认收货

        TextView bt_pay;//付款

        TextView bt_comment;//评价

        LinearLayout ll_bt;//底部
    }

    /**
     * listview条目
     */
    private class MyAdapter extends BaseAdapter {
        private List<OrderBean.DataBean.OrderlinesBean> details;

        public MyAdapter(List<OrderBean.DataBean.OrderlinesBean> details) {
            this.details = details;
        }

        @Override
        public int getCount() {
            return details.size();
        }

        @Override
        public Object getItem(int position) {
            return details.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewSonHolder sonHolder;
            if (convertView == null) {
                sonHolder = new ViewSonHolder();
                convertView = UiUtils.inflate(R.layout.item_order_goods);
                sonHolder.order_iv_heading = (ImageView) convertView.findViewById(R.id.order_iv_heading);
                sonHolder.order_tv_shopping_describe = (TextView) convertView.findViewById(R.id.order_tv_shopping_describe);
                sonHolder.order_tv_price = (TextView) convertView.findViewById(R.id.order_tv_price);
                sonHolder.order_tv_count = (TextView) convertView.findViewById(R.id.order_tv_count);
                sonHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                convertView.setTag(sonHolder);
            } else {
                sonHolder = (ViewSonHolder) convertView.getTag();
            }
            if (details.get(position).getProductname() != null) {
//                sonHolder.iv_goods_pic.setImageURI(Uri.parse(details.get(position).getPic1()));
            }
            sonHolder.order_tv_shopping_describe.setText(details.get(position).getProductname());

            sonHolder.order_tv_price.setText("" + details.get(position).getPrice());
            sonHolder.order_tv_count.setText("x " + details.get(position).getProductnum());
            Picasso.get().load(BaseContacts.BASE_URL + details.get(position).getPhoto()).into(sonHolder.order_iv_heading);


            return convertView;
        }
    }

    private class ViewSonHolder {
        ImageView order_iv_heading;

        TextView order_tv_shopping_describe;

        TextView tv_refund_ing;//退款中

        TextView tv_goods_size;

        TextView order_tv_price;

        TextView order_tv_count;

        TextView tv_money;//人民币符号
    }

    /**
     * 弹出对话框
     */
    private void initshowDialog() {
        picChooseDialog = new Dialog(context, R.style.pic_choose_dialog_progress);
        View view = LayoutInflater.from(context).inflate(
                R.layout.info_choose_dialog, null);
        final CheckBox checkbox_ch_info0 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info0);
        final CheckBox checkbox_ch_info1 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info1);
        final CheckBox checkbox_ch_info2 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info2);
        final CheckBox checkbox_ch_info3 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info3);
        final CheckBox checkbox_ch_info4 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info4);
        final CheckBox checkbox_ch_info5 = (CheckBox) view
                .findViewById(R.id.checkbox_ch_info5);
        final TextView ch_tv_order_commit = (TextView) view
                .findViewById(R.id.ch_tv_order_commit);


        checkbox_ch_info0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(true);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info0.getText().toString();
            }
        });
        checkbox_ch_info1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(true);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info1.getText().toString();
            }
        });
        checkbox_ch_info2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(true);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info2.getText().toString();
            }
        });
        checkbox_ch_info3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(true);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info3.getText().toString();
            }
        });
        checkbox_ch_info4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(true);
                checkbox_ch_info5.setChecked(false);
                mContent = checkbox_ch_info4.getText().toString();
            }
        });

        checkbox_ch_info5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_ch_info0.setChecked(false);
                checkbox_ch_info1.setChecked(false);
                checkbox_ch_info2.setChecked(false);
                checkbox_ch_info3.setChecked(false);
                checkbox_ch_info4.setChecked(false);
                checkbox_ch_info5.setChecked(true);
                mContent = checkbox_ch_info5.getText().toString();
            }
        });
        ch_tv_order_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkbox_ch_info0.isChecked()
                        || checkbox_ch_info1.isChecked()
                        || checkbox_ch_info2.isChecked()
                        || checkbox_ch_info3.isChecked()
                        || checkbox_ch_info4.isChecked()
                        || checkbox_ch_info5.isChecked()) {
                    requestApplyRefund(forList.get(cancelPosition).getData().get(cancelPosition).getOrderno(), mContent);
                    picChooseDialog.dismiss();

                } else {
                    ToastUitl.showShort("请先选择退款原因。");
                }
            }
        });

        Window window = picChooseDialog.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT,
                android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.popupWindowAnimation);
        picChooseDialog.setCanceledOnTouchOutside(true);
        picChooseDialog.show();
    }


    /**
     * 申请退款
     * @param orderNo
     */
    private void requestApplyRefund(String orderNo,String meno) {
        if (!NetWorkUtils.isNetConnected(context)) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(context).postUrlRequestApplyRefund(UserUtils.getUserToken(), orderNo,meno,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                forList.remove(cancelPosition);
                                notifyDataSetInvalidated();
                                if(forList.size() == 0){
                                    orderInterface.isRefreshAll(true);
                                }
                                ToastUitl.showShort("退款申请成功！");
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }
}
